package com.example.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

public class TalkAdapter extends ArrayAdapter<Talk> {
    public TalkAdapter(Context context, int resource, List<Talk> objects) {
        super(context, resource, objects);
    }

    @Override
    public View getView(int position,View convertView,ViewGroup parent) {
        Talk tal=getItem(position);
        View view;
        //新增一个内部类 ViewHolder，用于对控件的实例进行缓存
        ViewHolder viewHolder;
        if (convertView==null){
            //为每一个子项加载设定的布局
            view= LayoutInflater.from(getContext()).inflate(R.layout.act_layout,parent,false);
            viewHolder=new ViewHolder();
            viewHolder.name =view.findViewById(R.id.lv_item_act_name);
            viewHolder.content=view.findViewById(R.id.lv_item_act_content);
            viewHolder.time=view.findViewById(R.id.lv_item_act_time);
            view.setTag(viewHolder);//将 viewHolder 存储在 view 中
        }else {
            view=convertView;
            viewHolder= (ViewHolder) view.getTag();//重新获取 viewHolder
        }
        viewHolder.name.setText(tal.name);
        viewHolder.time.setText(tal.time);
        viewHolder.content.setText(tal.content);
        return view;
    }
    private class ViewHolder {
        TextView content,name,time;
    }
}
